package com.unicom.scg.scancodego.controller;



import com.unicom.scg.scancodego.entity.*;
import com.unicom.scg.scancodego.mapper.TOrderMapper;
import com.unicom.scg.scancodego.service.*;
import com.unicom.scg.scancodego.vo.PostFormGusetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * ClassName: UniCotroller <br/>
 * Description: <br/>
 * Date: 2019/10/28 10:05<br/>
 *
 * @author jason<br />
 * @version v1.0
 */
@Controller
public class UniCotroller {


    @Autowired
    TstaffService tstaffService;
    @Autowired
    TaddressService taddressService;
    @Autowired
    TproductService tproductService;
    @Autowired
    TorderService torderService;
    @Autowired
    VordersService vordersService;
    @Autowired
    TProcessService tProcessService;
    @RequestMapping("/userAdmin")
    public String userAdmin(Map<String,Object> map){
        List<TStaff> allUsers = tstaffService.getAllUsers();
        map.put("allUsersList",allUsers);
        return "useradmin";
    }
    @RequestMapping("/guestLogin")
    public String guestLogin(@RequestParam("addr8Id") Integer addr8Id,
                             Map<String, Object> map){
        TAddress tAddress = taddressService.selectById(addr8Id);
        map.put("addr8List",tAddress);
        List<TProduct> allProduct = tproductService.getAllProduct();
        map.put("allProductList",allProduct);
        return "guestindex";
    }
    @RequestMapping("/inputOrders")
    public String inputOrders(PostFormGusetVo postFormGusetVo,Map<String, Object> map){
        String fileName = postFormGusetVo.getResident_img_face().getOriginalFilename();
        if (postFormGusetVo.getResident_img_face().getSize()!= 0) {

            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID()+suffixName;
//            String filePath = "D:\\workspace\\Java\\scancodego\\src\\main\\resources\\static\\residentImg\\";
            String filePath = "D:\\upLoad\\";
            try {
                postFormGusetVo.getResident_img_face().transferTo(new File(filePath+fileName));
            } catch (Exception e) {
                e.printStackTrace();
                return "1上传失败," + e.getMessage();
            }
        }
        String fileName1 = postFormGusetVo.getResident_img_back().getOriginalFilename();
        if (postFormGusetVo.getResident_img_back().getSize()!= 0) {

            String suffixName1 = fileName1.substring(fileName1.lastIndexOf("."));
            fileName1 = UUID.randomUUID() + suffixName1;
//            String filePath1 = "D:\\workspace\\Java\\scancodego\\src\\main\\resources\\static\\residentImg\\";
            String filePath1 = "D:\\upLoad\\";
            try {
                postFormGusetVo.getResident_img_back().transferTo(new File(filePath1 + fileName1));
            } catch (Exception e) {
                e.printStackTrace();
                return "2上传失败," + e.getMessage();
            }
        }

        postFormGusetVo.setAddr8_id(postFormGusetVo.getAddr8_id());
        postFormGusetVo.setCustname(postFormGusetVo.getCustname());
        postFormGusetVo.setContacts_name(postFormGusetVo.getContacts_name());
        postFormGusetVo.setContacts_phone(postFormGusetVo.getContacts_phone());
        postFormGusetVo.setProduct_id(postFormGusetVo.getProduct_id());
        postFormGusetVo.setResident_id(postFormGusetVo.getResident_id());
        postFormGusetVo.setImg_face(fileName);
        postFormGusetVo.setImg_back(fileName1);
        torderService.saveOrder(postFormGusetVo);


        List<VOrders> ordersBytResident_id = vordersService.getOrdersBytResident_id(postFormGusetVo.getResident_id());
        map.put("orderList",ordersBytResident_id);
        return "success";
    }
    @RequestMapping("/queryOrders")
    public String queryOrders(@RequestParam("resident_id") String resident_id,
                              Map<String, Object> map){

        List<VOrders> ordersBytResident_id = vordersService.getOrdersBytResident_id(resident_id);
        map.put("queryOrderList",ordersBytResident_id);
        return "queryorder";
    }
    @RequestMapping("/procMonitor")
    public String pracMonitor(Map<String, Object> map){
        List<TProcess> allProc = tProcessService.getAllProc();
        map.put("allProcList",allProc);
        return "procmonitor";
    }
    @RequestMapping("/procAdmin")
    public String procAdmin(Map<String, Object> map){
        List<TProcess> allProc = tProcessService.getAllProc();
        map.put("allProcList",allProc);
        return "procadmin";
    }
}

